/**
 * @(#) com.kfx.net 2015/12/10;
 * <p>
 * Copyright (c), 2009 深圳孔方兄金融信息服务有限公司（Shenzhen kfxiong
 * Financial Information Service Co. Ltd.）
 * <p>
 * 著作权人保留一切权利，任何使用需经授权。
 */
package com.example.administrator.basedemos.net;

import android.app.Activity;
import android.support.annotation.NonNull;

import com.example.net.bean.BaseBean;
import com.google.gson.Gson;

import java.util.HashMap;


/**
 * Created by Syehunter on 2015/12/10.
 */
public class NetWorkHelper {
    private static final String TAG  = NetWorkHelper.class.getSimpleName();
    public static final  Gson   gson = new Gson();


    /**
     * 发送post请求
     *
     * @param url
     * @param paramas
     */
    public static void postRequest(@NonNull String url,
                                   @NonNull HashMap<String, String> paramas) {}


    /**
     * 处理response
     *
     * @param result    json
     * @param beanClazz JsonBean
     */
    public static void onResponse(@NonNull String result,
                                  @NonNull Class<? extends BaseBean> beanClazz) {
        onResponse(result, beanClazz, null);
    }


    /**
     * 处理response
     *
     * @param result                    json
     * @param beanClazz                 JsonBean
     * @param onResponseSuccessListener
     */
    public static void onResponse(@NonNull String result,
                                  @NonNull Class<? extends BaseBean> beanClazz,
                                  OnResponseSuccessListener onResponseSuccessListener) {
        onResponse(result, beanClazz, onResponseSuccessListener, null, null);
    }

    /**
     * 处理response
     *
     * @param result                    json
     * @param beanClazz                 JsonBean
     * @param activity                  指定返回ResultCode为需要重新登录时要弹出窗口的activiy
     * @param onResponseSuccessListener
     */
    public static void onResponse(@NonNull String result,
                                  @NonNull Class<? extends BaseBean> beanClazz,
                                  OnResponseSuccessListener onResponseSuccessListener,
                                  Activity activity) {
        onResponse(result, beanClazz, onResponseSuccessListener, null, activity);
    }

    /**
     * 处理response
     * --需要对某些错误的resultCode单独处理时调用此方法
     *
     * @param result                    json
     * @param beanClazz                 JsonBean
     * @param onResponseSuccessListener
     * @param onResponseFailureListener
     */
    public static void onResponse(@NonNull String result,
                                  @NonNull Class<? extends BaseBean> beanClazz,
                                  OnResponseSuccessListener onResponseSuccessListener,
                                  OnResponseFailureListener onResponseFailureListener) {
        try {
            onResponse(result, beanClazz, onResponseSuccessListener, onResponseFailureListener, null);
        } catch (Exception e) {
        }
    }

    /**
     * 处理response
     * --需要对某些错误的resultCode单独处理时调用此方法
     *
     * @param result                    json
     * @param beanClazz                 JsonBean
     * @param activity                  指定返回需要重新登录时要弹出窗口的activiy
     * @param onResponseSuccessListener
     * @param onResponseFailureListener
     */
    public static void onResponse(@NonNull String result,
                                  @NonNull Class<? extends BaseBean> beanClazz,
                                  OnResponseSuccessListener onResponseSuccessListener,
                                  OnResponseFailureListener onResponseFailureListener,
                                  Activity activity) {
        com.oxbix.beile.util.LogUtils.e(TAG, "返回数据 result == " + result);
    }


    /**
     * 处理失败信息
     *
     * @param e
     */
    public static void onFailure(Exception e) {

    }


    /**
     * 请求成功，resultCode = 0时调用
     */
    public interface OnResponseSuccessListener<T extends BaseBean> {
        void onResponseSuccess(T bean);
    }

    /**
     * 请求成功，resultCode != 0时调用
     */
    public interface OnResponseFailureListener<T extends BaseBean> {
        boolean onResponseFailure(T bean);
    }

    /**
     * response异常
     *
     * @param bean
     * @param onResponseFailureListener
     */
    private static void onResponseException(BaseBean bean, OnResponseFailureListener onResponseFailureListener) {
    }

    /**
     * 判断当前网络是否可用
     *
     * @return
     */
    public static boolean isNetworkAvailable() {
        return true;
    }

}
